package com.schrodinger.jdbc.connection.dbcp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.impl.GenericObjectPool;

public class DbcpDataSource {
	private String driver = null;
	private String url = null;
	private String name = null;
	private String password = null;
	private GenericObjectPool connectionPool = null;
	private String poolName ="";
	private static Class driverClass = null;
	private int maxActive;
	private int maxIdle;
	private int maxWait;
	private int minIdle;
	
	public int getMaxActive() {
		return maxActive;
	}

	public void setMaxActive(int maxActive) {
		this.maxActive = maxActive;
	}

	public int getMaxIdle() {
		return maxIdle;
	}

	public void setMaxIdle(int maxIdle) {
		this.maxIdle = maxIdle;
	}

	public int getMaxWait() {
		return maxWait;
	}

	public void setMaxWait(int maxWait) {
		this.maxWait = maxWait;
	}

	public int getMinIdle() {
		return minIdle;
	}

	public void setMinIdle(int minIdle) {
		this.minIdle = minIdle;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPoolName() {
		return poolName;
	}

	public void setPoolName(String poolName) {
		this.poolName = poolName;
	}
	//*********************************************************************************
	public DbcpDataSource() throws ClassNotFoundException {
		
	}
	
	private void initPool(String poolName) throws ClassNotFoundException {
		driverClass = Class.forName(driver);		
		if (connectionPool != null) {
			destoryPool();
		}
		try {
			connectionPool = new GenericObjectPool(null);
			connectionPool.setMaxActive(500);
			connectionPool.setMaxIdle(10);
			connectionPool.setMaxWait(2000);
			connectionPool.setMinIdle(5);
			ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
					url, name, password);
			PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
					connectionFactory, connectionPool, null, null, false, true);
			Class.forName("org.apache.commons.dbcp.PoolingDriver");
			PoolingDriver driver = (PoolingDriver) DriverManager
					.getDriver("jdbc:apache:commons:dbcp:");
			driver.registerPool(poolName, connectionPool);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void destoryPool() {
		try {
			PoolingDriver driver = (PoolingDriver) DriverManager
					.getDriver("jdbc:apache:commons:dbcp:");
			driver.closePool(poolName);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Connection getConnection() throws ClassNotFoundException {
		if (driverClass == null) {
			initPool(poolName);
		}
		Connection conn = null;
		try {
			conn = DriverManager
					.getConnection("jdbc:apache:commons:dbcp:" +poolName);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
}
